from django.shortcuts import render
import re
import random
from django.core.cache import cache
from rest_framework.views import APIView
from rest_framework import status
from rest_framework_simplejwt.tokens import RefreshToken
from apps.user.models import MyUser
from apps.user.serializers import UserInfoSerializer
from utils.custom_response import util_response
from utils.custom_send_mail import Sendmail, TYPE_MAIL
from utils.util import verify_mail


# Create your views here.


class GetEmailCode(APIView):
    """
    get:
    获取邮箱验证码
    """

    permission_classes = []
    authentication_classes = []

    def get(self, request):
        email = request.query_params.get('email')
        if not verify_mail(email):
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='请输入正确的邮箱地址！')
        r_code = cache.get('_'.join([TYPE_MAIL, email]))
        if r_code:
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='验证码已发送，请勿重复点击！')
        code = '{:06}'.format(random.randint(1, 999999))  # 随机生成六位验证码，前面补零
        # print(code)
        s = Sendmail(email, code)
        res = s.send_mail()
        if not res:
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='邮件发送失败！')
        cache.set('_'.join([TYPE_MAIL, email]), code, 60 * 15)
        return util_response()


class Login(APIView):
    """
    post:
    登录
    """

    permission_classes = []
    authentication_classes = []

    def post(self, request):
        email = request.data.get('email')
        code = request.data.get('code')
        if not verify_mail(email):
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='请输入正确的邮箱地址！')
        r_code = cache.get('_'.join([TYPE_MAIL, email]))
        if not r_code:
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='验证码失效，请重新获取！')
        if code != r_code:
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='验证码错误！')
        user_obj = MyUser.objects.filter(email=email).first()
        refresh = RefreshToken.for_user(user_obj)
        res_data = {
            "jwt_token": str(refresh.access_token),
            "type": "Bearer"
        }
        cache.delete('_'.join([TYPE_MAIL, email]))
        return util_response(data=res_data)


class Register(APIView):
    """
    post:
    注册
    """
    permission_classes = []
    authentication_classes = []

    def post(self, request):
        email = request.data.get('email')
        code = request.data.get('code')
        if not verify_mail(email):
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='请输入正确的邮箱地址！')
        r_code = cache.get('_'.join([TYPE_MAIL, email]))
        if not r_code:
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='验证码失效，请重新获取！')
        if code != r_code:
            return util_response(err=status.HTTP_400_BAD_REQUEST, msg='验证码错误！')
        MyUser.objects.create(email=email)
        cache.delete('_'.join([TYPE_MAIL, email]))
        return util_response()


class Info(APIView):
    """
    get:
    获取用户信息
    """

    def get(self, request):
        user = request.user
        res = UserInfoSerializer(user)
        return util_response(data=res.data)
